//
//  RadioViewController.m
//  片刻
//
//  Created by lanou3g on 15/6/3.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "RadioViewController.h"
#import "MaocHeader.h"
#import "AddCollectionViewCell.h"
#import "RadioModel.h"
#import "TilteReusableView.h"
#import "DetailsViewController.h"
@interface RadioViewController ()
@property (nonatomic,retain) UICollectionView *collec; //集合试图容器
@property (nonatomic,retain) UICollectionViewFlowLayout *layout; //布局
@property (nonatomic,retain) NSMutableArray *radioArr; //存放精选电台数据的数组
@property (nonatomic,retain) NSMutableArray *allArr; //存放全部电台的数组

@end

@implementation RadioViewController

-(void)dealloc
{
    self.collec = nil;
    self.layout = nil;
    self.radioArr = nil;
    self.allArr = nil;
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.naviLabel.text =@"电台";
    self.radioArr = [NSMutableArray array];
    self.allArr = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createData];
    [self createCollectionView];
    [self createButton];
}

//创建集合试图
-(void)createCollectionView
{

    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.minimumInteritemSpacing = 10;
    _layout.minimumLineSpacing = 10;
    _layout.itemSize = CGSizeMake(100, 120);    //大小
    _layout.headerReferenceSize = CGSizeMake(0, 40); //头部间距
    _layout.footerReferenceSize = CGSizeMake(0, 20);    //尾部间距
    
    _collec = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 70, SCREEN_WIDTH-20, SCREEN_HiGHT - 70) collectionViewLayout:_layout];
    _collec.backgroundColor = [UIColor whiteColor];
    _collec.delegate = self;
    _collec.dataSource = self;
    [_collec registerClass:[AddCollectionViewCell class] forCellWithReuseIdentifier:@"abc"];
    [_collec registerClass:[TilteReusableView class] forSupplementaryViewOfKind:@"UICollectionElementKindSectionHeader" withReuseIdentifier:@"tilte"];  //注册头部的View
    [self.view addSubview:_collec];
    
    [self.collec release];
    [self.layout release];
    
}
//创建数据
-(void)createData{
    RequesetManager *requeset = [[RequesetManager alloc] initWithRequestURL:@"http://api2.pianke.me/ting/radio" requestModel:POST parameyer:nil];
    requeset.parameyerDict=[@{@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":@"",@"version":@"3.0.2"} mutableCopy];
    requeset.delegate = self; //接受协议
    [requeset startRequest]; //开启请求
    
    [requeset release];
}
//创建button
-(void)createButton{
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(SCREEN_WIDTH - 100, 30, 100, 30);
    [button setTitle:@"我要当主播" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(zhuBoWithAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button];
}
-(void)zhuBoWithAction:(UIButton *)button
{
    NSLog(@"我要当主播");
}

#pragma mark - RequestDelegate
-(void)requestDidFinshWithData:(NSData *)data{
    NSDictionary *ditc = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
    NSDictionary *ditc1 = ditc[@"data"];
    NSArray *arr = ditc1[@"hotlist"];
    NSArray *alllistArr = ditc1[@"alllist"];
    for (NSDictionary *ditc in arr) {
        RadioModel *radio = [[RadioModel alloc] init];
        [radio setValuesForKeysWithDictionary:ditc];
        [self.radioArr addObject:radio];
        [radio release];
    }
    for (NSDictionary *dict in alllistArr) {
        RadioModel *radio = [[RadioModel alloc] init];
        [radio setValuesForKeysWithDictionary:dict];
        [self.allArr addObject:radio];
        [radio release];
    }
    [self.collec reloadData]; //刷新
}
-(void)requestError:(NSError *)error{
    NSLog(@"error:%@",error);
}



#pragma mark - UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.radioArr.count;
    };
    return  self.allArr.count;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsViewController *deta = [[DetailsViewController alloc] init];
    if (indexPath.section == 0) {
        RadioModel *model = self.radioArr[indexPath.row];
        deta.tingID = model.radioid;
        [self.navigationController pushViewController:deta animated:YES];
    }
    else
    {
        RadioModel *model = self.allArr[indexPath.row];
        deta.tingID = model.radioid;
        [self.navigationController pushViewController:deta animated:YES];
    }
}

//分区
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
         AddCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"abc" forIndexPath:indexPath];
    RadioModel *model = self.radioArr[indexPath.row];
    cell.model = model;
    return cell;
    }
    AddCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"abc" forIndexPath:indexPath];
    RadioModel *model = self.allArr[indexPath.row];
    cell.model = model;
    return cell;
    
   
}


//头部返回的 label.text
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    TilteReusableView *tilte = [collectionView dequeueReusableSupplementaryViewOfKind:@"UICollectionElementKindSectionHeader" withReuseIdentifier:@"tilte" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        tilte.label.text = @"精选电台  · Featured radio";
        [tilte.label setTextColor:[UIColor grayColor]];
        tilte.label.font = [UIFont systemFontOfSize:13];
        return tilte;
    }
    tilte.label.text = @"全部电台  · All Radios";
    [tilte.label setTextColor:[UIColor grayColor]];
    tilte.label.font = [UIFont systemFontOfSize:13];
    return tilte;
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
